{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "373839b3",
   "metadata": {},
   "source": [
    "# numpy模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "9da184c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5c1c599",
   "metadata": {},
   "source": [
    "1. array 创建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "c4fb2c69",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1,2,3])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "66bc60f5",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.size #查看数组元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "f45b51e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3,)"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape #查看形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "41a27c01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "660e202d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.itemsize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "271d3516",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [1, 2, 3]])"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([[1,2,3],[1,2,3]])\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "a3ebf806",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "adb42bae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "57e04dfd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.ndim"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae25160d",
   "metadata": {},
   "source": [
    "2. arange 函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "2fbc5b2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "93d3337d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "258d66e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 5, 7, 9])"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(1,10,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5a762fe",
   "metadata": {},
   "source": [
    "3.linspace 函数 (均匀分布)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "a83d9c2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1.,  12.,  23.,  34.,  45.,  56.,  67.,  78.,  89., 100.])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linspace(1,100,10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "052ad5a7",
   "metadata": {},
   "source": [
    "4.logspace（等比数列）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "d45a2d62",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 10.        ,  12.91549665,  16.68100537,  21.5443469 ,\n",
       "        27.82559402,  35.93813664,  46.41588834,  59.94842503,\n",
       "        77.42636827, 100.        ])"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.logspace(1,2,10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fdeb64b7",
   "metadata": {},
   "source": [
    "5. 其他"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "5aa392a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0.],\n",
       "       [0., 0., 0.]])"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros((2,3)) # 2行3列的数组,元素全部为0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "2e455336",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0., 0., 0.],\n",
       "       [0., 1., 0., 0., 0.],\n",
       "       [0., 0., 1., 0., 0.],\n",
       "       [0., 0., 0., 1., 0.],\n",
       "       [0., 0., 0., 0., 1.]])"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.eye(5) # 创建主对角线为1，其他元素为0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "cdd67cdc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 0],\n",
       "       [0, 2, 0],\n",
       "       [0, 0, 3]])"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.diag((1,2,3))# 创建对角矩阵数组，除对角线上的元素，其他元素为0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "6927de80",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1.],\n",
       "       [1., 1.],\n",
       "       [1., 1.]])"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones((3,2))# 3行2列的数组,元素全部为1,创建元素全部为1数组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fccf697",
   "metadata": {},
   "source": [
    "6. 数组数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "75f864cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int8')"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1,2,3],dtype=np.int8)\n",
    "a.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "03f2b801",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.int32(a)\n",
    "b.dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1c7e498",
   "metadata": {},
   "source": [
    "# 生成随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "cba35e1e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.78637687, 0.11307006, 0.21429409, 0.33624637, 0.3912003 ])"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.random(size=5) # 生成5个随机浮点型的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "516f8df2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0.85754532, 0.92155858, 0.95662692],\n",
       "        [0.04106637, 0.29751321, 0.57901045]],\n",
       "\n",
       "       [[0.1622372 , 0.8578865 , 0.81267606],\n",
       "        [0.59416507, 0.27935149, 0.61733836]]])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand(2,2,3) # 生成2个两行3列的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "56ab7e79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 1.19623481e+00,  8.69017312e-02,  2.11191969e+00],\n",
       "        [-8.83065487e-01, -2.89808126e-01,  1.40897125e+00]],\n",
       "\n",
       "       [[-8.60151976e-01, -2.58207016e-01, -2.28966480e+00],\n",
       "        [-5.36669318e-01,  1.15354757e-03,  4.39237612e-01]]])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randn(2,2,3) # 生成正态分布的随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "006e33af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 9],\n",
       "       [9, 7, 1]])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(0,10,size=(2,3)) # 生成0-10的随机数，两行三列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "179d69ab",
   "metadata": {},
   "source": [
    "# 通过索引访问数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "a252bff3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(10)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "81969f26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "4c81136b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "f7946d96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4])"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "68b2b49d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6, 7, 8])"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "f12ba013",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 5, 7])"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1:-1:2]  # 开始：结束： 步长"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "2b96650f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1, 12, 13,  4,  5,  6,  7,  8,  9])"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2:4] = 12,13\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e8607aa",
   "metadata": {},
   "source": [
    "# 多维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "d3a66c74",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 0, 2],\n",
       "       [1, 9, 0]])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.random.randint(0,10,size=(2,3))\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "169117bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 1])"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[:,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "d04cfcd4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 0, 2])"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[0,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "96b9a342",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0])"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[0,1:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "e5551d94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 2],\n",
       "       [1, 0]])"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[:,::2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39698c88",
   "metadata": {},
   "source": [
    "# 使用整数列索引访问多维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "c49ed9c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 2, 6, 8],\n",
       "       [8, 5, 7, 2],\n",
       "       [2, 9, 0, 1]])"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.random.randint(0,10,size=(3,4))\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "37b178d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 2])"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c[[0,2],[1,0]] # c[[行],[列]]  第0行1列，2行0列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f32b463",
   "metadata": {},
   "source": [
    "# 通过布尔值索引访问多维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "8938b7ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-122-ce3b665c2091>:1: DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.\n",
      "Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
      "  d = np.array([1,0,1],dtype=np.bool)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([ True, False,  True])"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = np.array([1,0,1],dtype=np.bool)\n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5260d227",
   "metadata": {},
   "source": [
    "# reshape 改变数组的形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "4650ff4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(12)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "aeae2340",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "fc200e71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4,  5],\n",
       "       [ 6,  7,  8,  9, 10, 11]])"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(12).reshape(2,6)\n",
    "a"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
